using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class JobSeekers_resumedetails: System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            connectionString =
                 WebConfigurationManager.ConnectionStrings[
                   "RecruitVietnam"].ConnectionString;
            if (Session.Contents["jsuserid"] == null)
            {
                Response.Redirect("login.aspx");
            }
            if (Session["ResumeID"] == null)
            {
                Response.Redirect("resumes.aspx");
            }
            else
                ResumeID.Value = 
                Session["ResumeID"].ToString();
            
            if (!IsPostBack)
            {
                LoadDataForDropDownList();           
                EditResume(Session["ResumeID"].ToString());
            }    
         }
        catch(Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    void LoadDataForDropDownList()
    {
        DropDownListHelper helper =
                new DropDownListHelper();
        Common cls = new Common(connectionString);
        DataSet dataSet = cls.GetDataSet(
        "select * from tblJobCategories; " +
            "Select * from tblJobIndustries;" +
            "select * from tblProvinces");
        DropDownList[] DropDownLists =
        new DropDownList[3] { ddlCategory, 
                ddlIndustry, ddlProvince };
        helper.FillDataTableToDropDownList(
        dataSet, DropDownLists,
        new string[3] { "JobCategoryName", 
                "JobIndustryName",
                "ProvinceName"},
        new string[3] { "JobCategoryID", 
                "JobIndustryID", "ProvinceID" },
        new string[3] { "", "", "" });
    }
      
    void EditResume(string resumeID)
    {
        if (resumeID != "" && resumeID != null )
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            cls.CommandText = "SPViewResumeDetails";
            cls.CommandType = CommandType.StoredProcedure;
            cls.ParameterCollection = new
            string[2] { "@UserID", "@ResumeID" };
            cls.ValueCollection = new
            string[2] { 
            Session["jsuserid"].ToString(), resumeID };
            DataRow dataRow =
            cls.GetDataRow();
            if (dataRow != null)
            {
                literalTitle.Text = dataRow["ResumeTitle"].ToString();  
                txtPosition.Text = dataRow[1].ToString();                
                ddlCategory.SelectedValue = dataRow[2].ToString();
                ddlIndustry.SelectedValue = dataRow[3].ToString();
                ddlProvince.SelectedValue = dataRow[4].ToString();
                txtCity.Text = dataRow[5].ToString();
                txtStartSalary.Text = dataRow[6].ToString();
                txtEndSalary.Text = dataRow[7].ToString();
                ddlCurrency.SelectedValue = dataRow[8].ToString();
                chkNegotive.Checked = ((dataRow[9].ToString() == "True")?true:false);
                chkRalocate.Checked = ((dataRow[10].ToString() == "True")?true:false);
                rblType.SelectedValue = dataRow[11].ToString();
                rblLevel.SelectedValue = dataRow[12].ToString();
                ddlStatus.SelectedValue = dataRow[13].ToString();
                txtDescription.Text = dataRow[14].ToString();
                btnNext.Text = "Update";
            }
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {

        try
        {
            string result = SaveResumeDetails();
            if(result == "")            
            {
                Session.Remove("ResumeID");
                if (btnNext.Text == "Update")
                {                
                    Response.Redirect("resumes.aspx");
                }
                else
                {                                     
                    Response.Redirect("thankyou.aspx");
                }                
            }
            else
                literalError.Text = result;
        }
        catch(Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
     
    string  SaveResumeDetails()
    {
        string errorMsg = "";
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = connectionString;
        if(!cls.AddNewOrUpdateResume(
        (btnNext.Text == "Save"),
        ResumeID.Value,
        txtPosition.Text,
        ddlCategory.SelectedValue,
        ddlIndustry.SelectedValue,
        ddlProvince.SelectedValue,
        txtCity.Text, txtStartSalary.Text,
        txtEndSalary.Text,
        ddlCurrency.SelectedValue,
        chkNegotive.Checked,
        chkRalocate.Checked,
        rblType.SelectedValue,
        rblLevel.SelectedValue,
        ddlStatus.SelectedValue,
        txtDescription.Text)) 
            errorMsg = cls.ErrorMessage;
        return errorMsg;
    }
}
